package com.frxuexi.reprot

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.reprot.databinding.ActivityReportBinding
import com.frxuexi.reprot.entity.ReportEntity
import kotlinx.coroutines.launch
import java.util.Date

@Route(path = "/report/ReportActivity")
class ReportActivity : AppCompatActivity() {
    lateinit var vm:ReportViewModel
    lateinit var db:ActivityReportBinding
    @SuppressLint("CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db=ActivityReportBinding.inflate(layoutInflater)
        setContentView(db.root)
        vm=ViewModelProvider(this).get(ReportViewModel::class.java)

        db.reportCom.setContent {
            var list by remember {
                mutableStateOf(ArrayList<ReportEntity>())
            }

            MyReportPage(list = list, {
                finish()
            }) {type,content,plan->
                val map = mapOf(
                    Pair("reportNowTitle", content),
                    Pair("reportNowValue", content),
                    Pair("reportNextTitle", plan),
                    Pair("reportNextValue", plan),
                    Pair("reportRemind", "赵先生"),
                    Pair("reportType", 1),
                    Pair("reportTime", Date().time)
                )
                lifecycleScope.launch{
                    vm.channel.send(ReportIntent.commitReport(map))
                }
            }


            lifecycleScope.launch{
                vm.reportState.collect{
                    when(it){
                        is ReportState.oGetSuccess->{
                            Log.i("===",it.datas.toString())
                            list = it.datas
                        }
                        is ReportState.onCommitSuccess->{
                            lifecycleScope.launch{
                                vm.channel.send(ReportIntent.getReport(mapOf(Pair("userId",1), Pair("page",1))))
                            }
                        }
                        is ReportState.onError->{
                            Log.i("===",it.msg)
                        }
                        else->{}
                    }
                }
            }
        }



        lifecycleScope.launch{
            vm.channel.send(ReportIntent.getReport(mapOf(Pair("userId",1), Pair("page",1))))
        }
    }
}